package com.ykdl.net.manager;

import java.io.File;
import java.util.Map;

import net.wxxr.http.config.HttpContans;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.request.HttpDeleteDataRequest;
import net.wxxr.http.request.HttpDeleteRequest;
import net.wxxr.http.request.HttpDownRequest;
import net.wxxr.http.request.HttpGetRequest;
import net.wxxr.http.request.HttpPostDataRequest;
import net.wxxr.http.request.HttpPostMutipleRequest;
import net.wxxr.http.request.HttpPostRequest;
import net.wxxr.http.request.HttpPutDataRequest;
import net.wxxr.http.request.HttpPutMutipleRequest;
import net.wxxr.http.request.HttpPutRequest;
import net.wxxr.http.request.HttpPutUploadByteRequest;
import oauth.signpost.http.HttpParameters;
import android.net.Uri;
import android.util.Log;

import com.ykdl.member.base.GlobalContext;
import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.util.AccessTokenKeeper;

/**
 * 请求模块，所有的请求都在这里面进行封装
 * @author yuanwenfei
 *
 */
public class Wxxr {
	private static Wxxr instace;
	private static AccessTokenKeeper mTokenKeeper;
	private static String HTTPTITLE_TOKEN_TITLE ,HTTPTITLE_TOKEN;
	private Wxxr() {}
	public static synchronized Wxxr getInstance() {
		if (instace == null) {
			instace = new Wxxr();
		}
		mTokenKeeper = new AccessTokenKeeper(GlobalContext.mContext);
		HTTPTITLE_TOKEN_TITLE =mTokenKeeper.getmAccessTokenTitle();
		HTTPTITLE_TOKEN =mTokenKeeper.getmAccessToken();
		Log.e("AccessToken", "--AccessToken:"+HTTPTITLE_TOKEN);
		return instace;
	}
	private static final String BASEURL = ""/*GlobalContext.PROJECT_SERVER*/;
	private static String currentTime = String.valueOf(System.currentTimeMillis());
	private static String deviceId = GlobalContext.getDeviceID();
	private final static String key = "wxxr";
	

	public HttpBaseRequest  postBaseRequestTest(String moduleUrl,HttpParameters parameterss,String seq){
		String url = BASEURL+moduleUrl/*getResultUrl(moduleUrl)*/;
		QLog.debug("xxxxxxxxxx url = ", url);
		HttpParameters   headParams = new HttpParameters();
	    try {
//	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
	    	headParams.put("access_token","fc4e13344ef789f58952064c99359f506189fa4a");
			HttpPostRequest  request = new HttpPostDataRequest();
			request.setUrl(url);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setBodyParams(parameterss);
			request.setHeadParams(headParams);
			request.setGzip(false);
			request.setNeedAuth(false);
			request.setRetry(false);
			request.setDebug(true);//打印数据或调试开关。true：打开，反之关闭
			return request;
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return null;
	}
	
	//以post方式请求
	public HttpBaseRequest  postBaseRequest(String moduleUrl,HttpParameters parameterss,String seq){
		String url = BASEURL+moduleUrl/*getResultUrl(moduleUrl)*/;
		QLog.debug("xxxxxxxxxx url = ", url);
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
			HttpPostRequest  request = new HttpPostDataRequest();
			request.setUrl(url);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setBodyParams(parameterss);
			request.setHeadParams(headParams);
			request.setGzip(false);
			request.setNeedAuth(false);
			request.setRetry(false);
			request.setDebug(true);//打印数据或调试开关。true：打开，反之关闭
			return request;
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return null;
	}
	
	//以get方式获得礼品资源
	public HttpBaseRequest getBaseRequest(String moduleUrl) {
		String url = url =BASEURL+moduleUrl /*getResultUrl(moduleUrl)*/;
		HttpParameters   headParams = new HttpParameters();
	    try {
	    headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
		HttpGetRequest  request = new HttpGetRequest();
		request.setHeadParams(headParams);
		request.setContentType(HttpContans.CONTENT_TYPE_MAP);
		request.setUrlParams(null);
		request.setUrl(url);
		request.setRetry(false);
		request.setGzip(false);
		request.setDebug(true);
		return request;
		
		
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	    return null;
	}
	
	public HttpBaseRequest getRequest(String url, HttpParameters urlParams){
		String stringurl = BASEURL+url;
		try {
			HttpGetRequest  request = new HttpGetRequest();
			HttpParameters   headParams = new HttpParameters();
			headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
//			headParams.put("access_token","4e95b364289218ec510a5ab533c4fbce26927e95");
			request.setHeadParams(headParams);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setBodyParams(null);
			request.setUrlParams(urlParams);
			request.setUrl(stringurl);
			request.setRetry(false);
			request.setGzip(false);
			request.setDebug(true);
			return request;
		 }catch(Exception e){
		    	e.printStackTrace();
		 }
		    return null;
	}

	public HttpBaseRequest getRequest(String url, HttpParameters urlParams, HttpParameters bodyParams){
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
			HttpGetRequest  request = new HttpGetRequest();
			request.setHeadParams(headParams);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setBodyParams(bodyParams);
			request.setUrlParams(urlParams);
			request.setUrl(url);
			request.setRetry(false);
			request.setGzip(false);
			request.setDebug(true);
			return request;
		 }catch(Exception e){
		    	e.printStackTrace();
		 }
		    return null;
	}
	public HttpBaseRequest getDownRequest(String url, HttpParameters urlParams, String saveFileNamePath){
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
			HttpDownRequest  request = new HttpDownRequest();
			request.setHeadParams(headParams);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setBodyParams(null);
			request.setUrlParams(urlParams);
			request.setUrl(url);
			request.setRetry(false);
			request.setGzip(false);
			request.setDebug(true);
			request.setSaveFilePath(saveFileNamePath);
			return request;
		 }catch(Exception e){
		    	e.printStackTrace();
		 }
		    return null;
	}
	public HttpBaseRequest getRequestTest(String moduleUrl,Map<String, String> data,String seq) {
		String url =BASEURL+moduleUrl /*getResultUrl(moduleUrl)*/;
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	Log.e("HTTPTITLE_TOKEN_TITLE", "----:"+HTTPTITLE_TOKEN);
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,"fc4e13344ef789f58952064c99359f506189fa4a");
	    	
		HttpGetRequest  request = new HttpGetRequest();
		request.setHeadParams(headParams);
		request.setContentType(HttpContans.CONTENT_TYPE_MAP);
		request.setUrlParams(null);
		url = buildGetMethod(url, data).build().toString();
		request.setUrl(url);
		request.setRetry(false);
		request.setGzip(false);
		request.setDebug(true);
		
		return request;
		
		
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	    return null;
	}
	public HttpBaseRequest getRequest(String moduleUrl,Map<String, String> data,String seq) {
		String url =BASEURL+moduleUrl /*getResultUrl(moduleUrl)*/;
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	Log.e("HTTPTITLE_TOKEN_TITLE", "----:"+HTTPTITLE_TOKEN);
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
	    	
		HttpGetRequest  request = new HttpGetRequest();
		request.setHeadParams(headParams);
		request.setContentType(HttpContans.CONTENT_TYPE_MAP);
		request.setUrlParams(null);
		url = buildGetMethod(url, data).build().toString();
		request.setUrl(url);
		request.setRetry(false);
		request.setGzip(false);
		request.setDebug(true);
		return request;
		
		
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
	    return null;
	}
	
	private static Uri.Builder buildGetMethod(String url,Map<String, String> data) {
        final Uri.Builder builder = new Uri.Builder();
        builder.encodedPath(url);
        
		if(data != null){
			for(Map.Entry<String, String> m:data.entrySet()){
				builder.appendQueryParameter(m.getKey(), m.getValue());
			}
		}	
        return builder;
    }
	public HttpBaseRequest deltteRequest(String moduleUrls,HttpParameters parameterss){
		String url =BASEURL+moduleUrls /*getResultUrl(moduleUrl)*/;
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
	    	HttpDeleteRequest request = new HttpDeleteDataRequest();
	    	request.setHeadParams(headParams);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setUrlParams(null);
			request.setBodyParams(parameterss);
			request.setUrl(url);
			request.setRetry(false);
			request.setGzip(false);
			request.setDebug(true);
			return request;
	    	
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		return null;
		
	}
	public HttpBaseRequest putRequest(String moduleUrls,HttpParameters parameterss,String seq){
		String url =BASEURL+moduleUrls /*getResultUrl(moduleUrl)*/;
		HttpParameters   headParams = new HttpParameters();
	    try {
	    	headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
//	    	headParams.put("access_token","ff5ff60a63df44e479bf0ae007f0583bc24a00e3");
	    	HttpPutRequest request = new HttpPutDataRequest();
	    	request.setHeadParams(headParams);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setUrlParams(null);
			request.setBodyParams(parameterss);
			request.setUrl(url);
			request.setRetry(false);
			request.setGzip(false);
			request.setDebug(true);
			return request;
	    	
	    }catch(Exception e){
	    	e.printStackTrace();
	    }
		return null;
		
	}
	
	/**
	 * 以Post方式 上传文件，如img  vidow File等
	 * @param url
	 * @param bodyData
	 * @param pwd
	 * @param filePathName
	 * @return
	 */
	public HttpBaseRequest uploadImage(String url,String bodyData,String pwd,String filePathName){
		try {
			
			/**动态添加url参数*/
			HttpParameters  urlParams = new HttpParameters();
			//urlParams.put(key, value);
			//urlParams.put(key, value);
			//urlParams.put(key, value);
			
			/**动态添加消息头*/
			HttpParameters  headParams = new HttpParameters();
			//headParams.put("lmyr", currentTime);
			//String md5 = SecureConnUtils.encode(deviceId, currentTime, key);
			//headParams.put("lmyrmd5", md5);
			//headParams.put("email", "");
			//headParams.put("pwd", pwd);
			
			/**设置Request请求对象*/
			
			HttpPutUploadByteRequest  request = new HttpPutUploadByteRequest();
			//request.setUrl(url);//设置url
			//request.setUrlParams(urlParams);//设置url参数
			//request.setBodyData(bodyData);//设置实体数据，以 String 类型的格式,如果是json，则使用String类型
			//request.setBodyParams(bodyParams);//设置实体数据，以HttpParames类型的格式
			//request.setContentType(HttpContans.CONTENT_TYPE_JSON);//发送内容格式类型。如 json  xml  key=value 等
			request.setFilePathName(filePathName);//上传文件完整路径名
//			request.setHeadParams(headParams);//设置消息头
			//request.setRetry(false);//请求失败，是否进行刷新请求
			//request.setNeedAuth(false);//设置认证信息。
			//request.setGzip(false);//对相应的数据是否进行解压
			
			return request;
			
		} catch (Exception e) {
			return null;
		}
		
	}
	
	
	//post方式发送文件请求  ---数据构成：文件，数据+文件，数据。如向服务器上传或发送   内容+图片
		public HttpBaseRequest  uploadFile(String moduleUrls,File file){
			String url =BASEURL+moduleUrls;
			HttpParameters   headParams = new HttpParameters();
			try {
				headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
//			headParams.put("access_token","61d0a5abe8bb0a61d4ed19a665d35f7c915979d9");
			HttpPutMutipleRequest  request = new HttpPutMutipleRequest();
			//动态添加参数
			HttpParameters  parameters = new HttpParameters();
			parameters.put("temp_id", "123456");
			parameters.put("target", "groups");
			request.setBodyParams(parameters);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setHeadParams(headParams);
//			request.setBodyParams(parameters);
			request.setFile(file);
			request.setFileKey("file");//文件类型
			request.setUrl(url);
			return request;
		} catch (Exception e) {
			return null;
		}
		}
		
		public HttpBaseRequest  uploadAvatar(String moduleUrls,File file){
			String url =BASEURL+moduleUrls;
			HttpParameters   headParams = new HttpParameters();
			try {
				headParams.put(HTTPTITLE_TOKEN_TITLE,HTTPTITLE_TOKEN);
//			headParams.put("access_token","61d0a5abe8bb0a61d4ed19a665d35f7c915979d9");
			HttpPostMutipleRequest  request = new HttpPostMutipleRequest();
			//动态添加参数
			HttpParameters  parameters = new HttpParameters();
			request.setBodyParams(parameters);
			request.setContentType(HttpContans.CONTENT_TYPE_MAP);
			request.setHeadParams(headParams);
//			request.setBodyParams(parameters);
			request.setFile(file);
			request.setFileKey("file");//文件类型
			request.setUrl(url);
			return request;
		} catch (Exception e) {
			return null;
		}
		}
		
	
		
}
